<?php if (isset($success_message)) : ?>
    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text">Guia creada con exito!</span></div>
    </div>
<?php endif; ?>
<div class="noty_bar noty_theme_default noty_layout_topCenter noty_error" id="message_error_formAddProducts" style="cursor: pointer; left: 559.5px;">
    <div class="noty_message"><span id="contentMessageErrorFormAddProducts" class="noty_text">El codigo ya existe para otro producto!</span></div>
</div>


<script>
                                        $(document).ready(function() {
                                            var valor = $('#valor').val();
                                            if(valor == 1)
                                             {
                                                 
                                             }
                                             else
                                            {
                                              
                                            $('#code_product').focus(function() {
                                                $('#guardar_guia').click();                                                
                                              }); 
                                            }                                                                                                                                  
                                            $('#guardar_guia').click(function(e) {
                                                                                                                                                                                                                                                              
                                            $("#formAddProducts").validate({
                               rules: {
                                   code: {           //input name: fullName
                                       required: true  //required boolean: true/false                                             
                                   }
                               
                               },
                               messages: {               //messages to appear on error
                                   code: {
                                         required:"Debe ingresar su código"                                         
                                         }
                                
                               }                              
                           });                                                      
                                            });                                                                                        
                                        })
                                    </script> 


<div class="grid_container">
    
        
    <?=validation_errors('<div class="errors">','</div>'); ?>
    <?php
    $attributes = array(
        'id' => 'formAddProducts',
        'class' => 'form_container'
    );
    echo form_open_multipart('products/submit', $attributes);?>
    
        <div class="grid_12">
                <div class="widget_wrap">
                        <div class="widget_top">
                                <span class="h_icon money"></span>
                                <h6><? ECHO $estado = isset($product->code)? 'Editar' : 'Agregar' ?> Producto</h6>
                                <input name="estado" id="estado" type="hidden" value="<? echo $estado?>">
                                   <?
                                if(isset($estado))
                                {
                                    ?>
                                <input type="hidden" name="valor" id="valor" value="1">
                                    <?
                                }
                                ?>
                        </div>
                        <div class="widget_content">
                                <ul>
                                <!-- <?php /*if (!isset($product->code)) : */?> -->
                                        <li>
                                                <div class="form_grid_12" >
                                                        <div class="form_input" >
                                                                <div class="form_grid_12 alpha"> 
                                                                        <span class="field_title" > Código<span class="req"  >*</span></span>
                                                                         
                                                                <?php


                                                                        if(isset($product->code)){
                                                                        $data = array(
                                        'readonly'  => 'readonly',
                                        'name'        => 'code',
                                        'id'          => 'code_product',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('code'), $product->code),
                                        'tabindex'    => '1',
                                        'required'
                                    );
                                    echo form_input($data);
                                }else{ ?>
                                         <input type="text" name="code" id="code_product" class="noEmpty" value="<?php echo @field(set_value('code'), $product->code); ?>" tabindex="1" required>
                              <?php  }
                                    ?>
                                                                </div> 
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                <?php /*endif; */ ?>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Descripcion <span class="req">*</span></span>
                                                                         <input type="text" name="description" id="description" required class="noEmpty" value="<?php echo @field(set_value('description'), $product->description); ?>" tabindex="2" >

                                                                <!--    <?php
                                /*    $data  /* = array(
                                        'name'        => 'description',
                                        'id'          => 'description',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('description'), $product->description),
                                        'tabindex'    => '2',
                                    );
                                    echo form_input($data);*/
                                    ?> -->
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Categoría<span class="req">*</span></span>
                                                                        <?php 
                                                                            $options = array();
                                                                            $options[-1] = "";
                                                                            foreach ($productGroups as $pg) {
                                                                                    $options[$pg->id] = $pg->description;
                                                                                }

                                                                                $extra = 'id="client" tabindex="3" style="width:350px"  required class="chzn-select full" data-placeholder="Elegir..."';


                                                                                echo form_dropdown('group_id', $options, @field(set_value('group_id'), $product->group_id), $extra);
                                                                        ?>
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                        <?php if (!isset($product->code)) : ?>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Fecha de Registro <span class="req">*</span></span>
                                                                        <?php
                                    $data = array(
                                        'disabled'    =>'disabled',
                                        'name'        => 'insert_date',
                                        'id'          => 'insert_date',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('insert_date'), $product->insert_date, date('d/m/Y')),
                                        'tabindex'    => '4',
                                        'OnFocus'         => 'this.blur()'
                                    );
                                    echo form_input($data);
                                    ?>
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                        <?php endif; ?>
                                </ul>
                        </div>
            </div>
                <div class="widget_wrap">
                        <div class="widget_top">
                                <span class="h_icon list"></span>
                                <h6>Unidad por mayor (empaque)</h6>
                        </div>
                        <div class="widget_content">
                                <ul>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Descripcion <span class="req">*</span></span>
                                                                         <input type="text" name="pack_desc" id="pack_desc" required class="noEmpty" value="<?php echo  @field(set_value('pack_desc'), $product->pack_desc); ?>" tabindex="2" >
                                                                        <!-- <?php
                                   /* $data = array(
                                        'name'        => 'pack_desc',
                                        'id'          => 'pack_desc',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('pack_desc'), $product->pack_desc),
                                        'tabindex'    => '5',
                                    );
                                    echo form_input($data);*/
                                    ?>  -->
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Unidades que contiene<span class="req">*</span></span>
                                                                         <input type="text" name="pack_units" id="pack_units" required class="onlyNumber" value="<?php echo  @field(set_value('pack_units'), $product->pack_units); ?>" tabindex="2">
                                                                        <!-- <?php
                                   /* $data = array(
                                                                        <!-- <?php
                                   /* $data = array(
                                        'name'        => 'pack_units',
                                        'id'          => 'pack_units',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('pack_units'), $product->pack_units),
                                        'tabindex'    => '6',
                                    );
                                    echo form_input($data);
                                    */?>  -->
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">x Caja <span class="req">*</span></span>
                                                                         <input type="text" name="box_packs" id="box_packs" required class="onlyNumber" value="<?php echo  @field(set_value('box_packs'), $product->box_packs); ?>" tabindex="2">
                                                                        <!-- <?php
                                    /*$data = array(
                                        'name'        => 'box_packs',
                                        'id'          => 'box_packs',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('box_packs'), $product->box_packs),
                                        'tabindex'    => '7',
                                    );
                                    echo form_input($data);
                                    */?> -->
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Costo US$ <span class="req">*</span></span>
                                                                         <input type="text" name="cost" id="cost" required class="onlyNumber" value="<?php echo  @field(set_value('cost'), $product->cost); ?>" tabindex="2">

                                                                        <!-- <?php
                                    /*$data = array(
                                        'name'        => 'cost',
                                        'id'          => 'cost',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('cost'), $product->cost),
                                        'tabindex'    => '8',
                                    );
                                    echo form_input($data);
                                   */ ?> -->
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Precio Venta US$ <span class="req">*</span></span>
                                                                        <input type="text" name="price" id="price" required class="onlyNumber" value="<?php echo  @field(set_value('price'), $product->price); ?>" tabindex="2">
                                                                        <!-- <?php
                                   /* $data = array(
                                        'name'        => 'price',
                                        'id'          => 'price',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('price'), $product->price),
                                        'tabindex'    => '9',
                                    );

                                   echo form_input($data);
                                    */?> -->
                                                                </div>
                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                </ul>
                                <span class="clear"></span>
                        </div>
                </div>
                <div class="widget_wrap">
                        <div class="widget_top">
                                <span class="h_icon list"></span>
                                <h6>Foto</h6>
                        </div>
                        <div class="widget_content">
                                <ul>
                                        <li>
                                                <div class="form_grid_12">
                                                        <div class="form_input">
                                                                <div class="form_grid_12 alpha">
                                                                        <span class="field_title">Ruta <span class="req">*</span></span>
                                                                        <?php 
/* @var $product type */                                                   //subida de imagen                    
                                                                        if(isset($product->code))
                                                                        { ?>
                                                                        <img src="<?php  echo base_url()."images/products/".$product->picture; ?>" width="180" height="214"><br>
                                                                       <?php  } 
                                                                                                 
                                                                        
                                    $data = array(
                                     
                                        'name'        => 'path_image',
                                        'id'          => 'path_image',
                                        'tabindex'    => '10',
                                        'value'        =>  @field(set_value('path_image'), $product->picture),
                                        
                                    );
                                    echo form_upload($data);
                                    
                                    ?> 
                                                                </div>

                                                                <span class="clear"></span>
                                                        </div>
                                                </div>
                                        </li>
                                </ul>
                                <span class="clear"></span>
                        </div>
                </div>
                <br />
        <!-- 
                <form method="POST" action="products_form.php">

<?php 
/*$conexion=mysql_connect('localhost','root','') or die('No hay conexi�n a la base de datos');
$db=mysql_select_db('base',$conexion)or die('no existe la base de datos.');

$consulta=mysql_query("select picture from products");
while($filas=mysql_fetch_array($consulta)){
        $ruta=$filas['picture'];
        */


?>  --> 
<script type="text/javascript">
                    $(document).ready(function() {

                        function checkCode() {

                            $.ajax({
                                'url': url + '/products/evalue',
                                'type': 'POST',
                                'data': {'code': $('input[name=code]').val()},
                                'success': function(res) {
                                   // alert("alert res: " + res);
                                    if (res == "true") {                                        
                                       $('div[id=noty_success_1355937644582]').css('display', 'none');
                                       $('div[id=message_error_formAddProducts]').css('display', 'block');
                                    } else if (res == "false") {       
                                       
                                       $('#formAddProducts').submit();
                                    }
                                }
                            });

                        }

                        $('#code').focus(function() {
                            $('#code').css('background', '#ffffff');
                        })
                        $('input[name=date]').focus(function() {
                            $('input[name=date]').css('background', '#ffffff');
                        })


                        function formtrue() {
                            $('formAddProducts').submit(function() {
                                return true;
                            });
                        }
                        function formfalse() {
                            $('formAddProducts').submit(function() {
                                return false;
                            });
                        }

                        $('#guardar_guia').click(function() {  
                                    if($('input[name=estado]').val()=="Editar")
                                    {
                                        $('#formAddProducts').submit();
                                    }
                                    else
                                    {
                                        checkCode(); /*funcion ajax submit*/
                                    }
                                    
                        });
                    })
                </script>
                <button type="button" id="guardar_guia" class="btn_small btn_orange"><span>Guardar</span></button> </form>
                <button type="button" class="btn_small btn_orange"><span><a id="buscar_guia" href="<?php echo base_url(); ?>index.php/products/">Cancelar</a></span></button>
                
                <!-- <button type="button" class="btn_small btn_orange" onclick="javascript:window.location='<?=base_url(); ?>products'"><span>Regresar</span></button> -->
        </div>
    <?php echo form_close(); ?>
        <span class="clear"></span>
</div>
